<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * 后台管理主页面
 * @author 		jaypha
 * @qq			168007694
 * @email		psufool@163.com
 * @home page	www.psufool.cn
 */
class Channel extends Admin_Controller{

	function __construct(){
		parent::__construct();
		$this->load->model('JayphaDB');
		$this->load->model('JayphaTemplate');
		$this->load->dbforge();
		if(is_null($this->adminCurrentTemplate) or empty($this->adminCurrentTemplate)){
			$this->adminCurrentTemplate = 'default';
		}
		$this->dbTablePrefix = $this->config->item('db_table_prefix');
		$this->datatype = array(
			'text'=>'单行文本(varchar)',
			'textchar'=>'单行文本(char)',
			'multitext'=>'多行文本',
			'htmltext'=>'HTML文本',
			'textdata'=>'文本保存HTML数据',
			'int'=>'整数类型',
			'float'=>'小数类型',
			'datetime'=>'时间类型',
			'img'=>'图片',
			'imgfile'=>'图片(仅网址)',
			'media'=>'多媒体文件',
			'addon'=>'附件类型',
			'select'=>'使用select下拉框',
			'radio'=>'使用radio单选框',
			'checkbox'=>'使用checkbox多选框'
		);
		$this->datacate = array(
			'text'=>'varchar',
			'textchar'=>'char',
			'multitext'=>'varchar',
			'htmltext'=>'mediumtext',
			'textdata'=>'mediumtext',
			'int'=>'int',
			'float'=>'float',
			'datetime'=>'int',
			'img'=>'varchar',
			'imgfile'=>'varchar',
			'media'=>'varchar',
			'addon'=>'varchar',
			'select'=>'set',
			'radio'=>'set',
			'checkbox'=>'set'
		);
	}

	function index(){
		parse_str($this->input->server('QUERY_STRING'),$this->input->get());
		if(!$this->noLoginErr()) return false;
		$data['adminCurrentTemplate'] = $this->adminCurrentTemplate;
		$tvName = 'channel';
		$condition = array();
		if($this->input->server('REQUEST_METHOD')=='POST'){
			$active = $this->input->post('active');
			$del = $this->input->post('del');
			$id = $this->input->post('id');
			if(is_array($id)){
				$id = implode(',',$id);
			}
			$data['disabled'] = $this->input->post('disabled');
			$data['title'] = $this->input->post('title');
			$data['createTimeStart'] = $this->input->post('createTimeStart');
			$data['createTimeEnd'] = $this->input->post('createTimeEnd');
		}else{
			$data['disabled'] = $this->input->get('disabled');
			$data['title'] = $this->input->get('title');
			$data['createTimeStart'] = $this->input->get('createTimeStart');
			$data['createTimeEnd'] = $this->input->get('createTimeEnd');
			//list($data['rDisabled'],$data['rName'],$data['rTitle'],$data['rCreateTimeStart'],$data['rCreateTimeEnd']) = explode('~',$this->uri->segment(4,'~~~~'));
		}

		$data['title'] = urldecode($data['title']);

		$condition = array();
		$db_like_key = array();
		$db_like_value = array();
		if(strlen($data['disabled']>0)){
			$condition['disabled'] = $data['disabled'];
		}
		if(strlen($data['title'])>0){
			array_push($db_like_key,'title');
			array_push($db_like_value,$data['title']);
		}
		if(strlen($data['createTimeStart'])>0){
			$condition['createTime >= '] = $data['createTimeStart'];
		}
		if(strlen($data['createTimeEnd'])>0){
			$condition['createTime <= '] = date('Y-m-d',strtotime($data['createTimeEnd']) + 24*60*60);
		}
		if(count($db_like_key)>0){
			$condition['db_like'] = array_combine($db_like_key,$db_like_value);
		}

		unset($db_like_key);
		unset($db_like_value);
		$where = $condition;

		if($this->input->server('REQUEST_METHOD')=='POST'){
			if(strtoupper($active)=='DEL'){
				if(strlen($del)==0){
					$condition['db_where_in'] = array('id',$id);
					$del_table_list = $this->JayphaDB->getDB('','','subjoinTable',$this->dbTablePrefix.$tvName,$condition,'',true);
				}else{
					$del_table_list = $this->JayphaDB->getDB('','','subjoinTable',$this->dbTablePrefix.$tvName,$where,'',true);
				}
				$operationData = array(
					'adminId'=>$this->session->userdata('adminUserId'),
					'title'=>'删除内容模型',
					'url'=>$this->input->server('REQUEST_URI'),
					'remark'=>'删除系内容，删除内容：'.json_encode($this->JayphaDB->getSingleDB($this->dbTablePrefix.$tvName,$condition))
				);
				$this->db->trans_start();
				//$this->JayphaDB->setAdminOperation($operationData);
				if(strlen($del)==0){
					$this->JayphaDB->delDB($this->dbTablePrefix.'category',array('db_where_in'=>array('channelId',$id)));
					$this->JayphaDB->delDB($this->dbTablePrefix.'archives',array('db_where_in'=>array('channelId',$id)));
				}else{
					$del_channelIdList = $this->JayphaDB->getDB(0,0,'id',$this->dbTablePrefix.$tvName,$condition,'',false);
					$del_channelId = array();
					for($i=0;$i<count($del_channelIdList);$i++){
						$del_channelId[$i] = $del_channelIdList[$i]->id;
					}
					unset($del_channelIdList);
					$del_channel_condition = array('db_where_in'=>array('channelId',$del_channelId));
					unset($del_channelId);
					$this->JayphaDB->delDB($this->dbTablePrefix.'category',$del_channel_condition);
					$this->JayphaDB->delDB($this->dbTablePrefix.'archives',$del_channel_condition);
				}
				$this->JayphaDB->delDB($this->dbTablePrefix.$tvName,'');		
				foreach($del_table_list as $table){
					if(strlen($table->subjoinTable)!=0)
						$this->dbforge->drop_table($this->dbTablePrefix.$table->subjoinTable);
				}
				$this->db->trans_complete();			
			}
		}

		unset($condition);

		$config['base_url'] = site_url("admin/channel/index/");
		$config['total_rows'] = $this->JayphaDB->getDBCount($this->dbTablePrefix.$tvName,$where);
		$config['per_page'] = $this->JayphaDB->getConfigValue('adminPageSize');
		$config['uri_segment'] = 4;
		$config['num_links'] = 5;
		//$config['full_tag_open'] = '<div>';
		//$config['full_tag_close'] = '</div>';
		$config['first_link'] = '|<';
		$config['last_link'] = '>|';
		$config['next_link'] = '>>';
		$config['prev_link'] = '<<';

		$this->pagination->initialize($config);

		$data['currentSite'] = '频道管理<span class="split">>></span>内容模型管理<span class="split">>></span>内容模型列表';
		$data['phpSelf'] = $config['base_url'];
		$data['pageTag'] = '内容模型管理';
		$data['editPage'] = site_url('admin/channel/edit');
		$data['editTitle'] = '修改内容模型';
		$data['addTitle'] = '添加内容模型';
		$data['perm'] = 'channel';
		$data['adminPermission'] = $this->JayphaDB->getAdminPermission($this->session->userdata('adminUserId'));
		$data['tvName'] = $tvName;
		$order = 'id';
		$num = $this->uri->segment(4,0);
		$data['channelList'] = $this->JayphaDB->getDB($config['per_page'],$num,'',$this->dbTablePrefix.$tvName,$where,$order,false);
		$this->load->view("admin/$this->adminCurrentTemplate/channel",$data);
	}

	function edit(){
		if(!$this->noLoginErr()) return false;
		$data['adminCurrentTemplate'] = $this->adminCurrentTemplate;
		$tvName = 'channel';
		$data['tvName'] = $tvName;
		$data['perm'] = 'channel';
		$data['db_table_prefix'] = $this->dbTablePrefix;
		$data['datatype'] = $this->datatype;		
		if($this->input->server('REQUEST_METHOD')=='POST'){
			$data['id'] = $this->input->post('id');
			$data['disabled'] = $this->input->post('disabled');
			$data['title'] = $this->input->post('title');
			$data['identifier'] = $this->input->post('identifier');
			$data['subjoinTable'] = $this->input->post('subjoinTable');
			$data['useTitle'] = $this->input->post('useTitle');
			$data['listfields'] = $this->input->post('listfields');
			$data['adminEditCon'] = $this->input->post('adminEditCon');
			$data['adminMainCon'] = $this->input->post('adminMainCon');
			$data['userMainCon'] = $this->input->post('userMainCon');
			$data['userEditCon'] = $this->input->post('userEditCon');
			$data['fieldset'] = $this->input->post('fieldset');
			if(strlen($data['adminMainCon'])>0){
				if(substr($data['adminMainCon'],0,1)=='/') $data['adminMainCon'] = substr($data['adminMainCon'],1);
			}
			if(strlen($data['adminMainCon'])>0){
				if(substr($data['adminMainCon'],strlen($data['adminMainCon'])-1)=='/') $data['adminMainCon'] = substr($data['adminMainCon'],0,strlen($data['adminMainCon'])-1);
			}
			if(strlen($data['title'])==0){
				unset($data['errorScript']['lhgdialog_alert_mess']);
				$data['errorScript'] = array('lhgdialog_alert_mess'=>array(
						'width'=>320,
						'height'=>108,
						'title'=>'名称错误',
						'content'=>'<span class="red">对不起，频道名称不能为空，请输入频道名称！</span>',
						'url'=>'history.back();',
						'target'=>'_self',
						'conver'=>true,
						'resize'=>false,
						'drag'=>false,
						'btns'=>true,
						'btnText'=>'返回',
						'rightClose'=>false
				));
				$this->load->view("admin/$this->adminCurrentTemplate/error",$data);
				return false;
			}
			if($this->JayphaDB->getDBCount($this->dbTablePrefix.$tvName,array('id != '=>$data['id'],'upper(title)'=>strtoupper($data['title'])))>0){
				unset($data['errorScript']['lhgdialog_alert_mess']);
				$data['errorScript'] = array('lhgdialog_alert_mess'=>array(
						'width'=>320,
						'height'=>108,
						'title'=>'频道名称错误',
						'content'=>'<span class="red">对不起，频道名称已经存在，请重新输入！</span>',
						'url'=>'history.back();',
						'target'=>'_self',
						'conver'=>true,
						'resize'=>false,
						'drag'=>false,
						'btns'=>true,
						'btnText'=>'返回',
						'rightClose'=>false
				));
				$this->load->view("admin/$this->adminCurrentTemplate/error",$data);
				return false;
			}
			if(strlen($data['identifier'])==0){
				unset($data['errorScript']['lhgdialog_alert_mess']);
				$data['errorScript'] = array('lhgdialog_alert_mess'=>array(
						'width'=>300,
						'height'=>108,
						'title'=>'标识符错误',
						'content'=>'<span class="red">对不起，标识符不能为空，请输入标识符！</span>',
						'url'=>'history.back();',
						'target'=>'_self',
						'conver'=>true,
						'resize'=>false,
						'drag'=>false,
						'btns'=>true,
						'btnText'=>'返回',
						'rightClose'=>false
				));
				$this->load->view("admin/$this->adminCurrentTemplate/error",$data);
				return false;
			}
			if($this->JayphaDB->getDBCount($this->dbTablePrefix.$tvName,array('id != '=>$data['id'],'upper(identifier)'=>strtoupper($data['identifier'])))>0){
				unset($data['errorScript']['lhgdialog_alert_mess']);
				$data['errorScript'] = array('lhgdialog_alert_mess'=>array(
						'width'=>320,
						'height'=>108,
						'title'=>'标识符错误',
						'content'=>'<span class="red">对不起，标识符已经存在，请重新输入标识符！</span>',
						'url'=>'history.back();',
						'target'=>'_self',
						'conver'=>true,
						'resize'=>false,
						'drag'=>false,
						'btns'=>true,
						'btnText'=>'返回',
						'rightClose'=>false
				));
				$this->load->view("admin/$this->adminCurrentTemplate/error",$data);
				return false;
			}
			if(strlen($data['subjoinTable'])==0){
				unset($data['errorScript']['lhgdialog_alert_mess']);
				$data['errorScript'] = array('lhgdialog_alert_mess'=>array(
						'width'=>300,
						'height'=>108,
						'title'=>'附加表错误',
						'content'=>'<span class="red">对不起，附加表不能为空，请输入附加表！</span>',
						'url'=>'history.back();',
						'target'=>'_self',
						'conver'=>true,
						'resize'=>false,
						'drag'=>false,
						'btns'=>true,
						'btnText'=>'返回',
						'rightClose'=>false
				));
				$this->load->view("admin/$this->adminCurrentTemplate/error",$data);
				return false;
			}
			if($this->JayphaDB->getDBCount($this->dbTablePrefix.$tvName,array('id != '=>$data['id'],'upper(subjoinTable)'=>strtoupper($data['subjoinTable'])))>0){
				unset($data['errorScript']['lhgdialog_alert_mess']);
				$data['errorScript'] = array('lhgdialog_alert_mess'=>array(
						'width'=>320,
						'height'=>108,
						'title'=>'附加表错误',
						'content'=>'<span class="red">对不起，附加表已经存在，请重新输入附加表！</span>',
						'url'=>'history.back();',
						'target'=>'_self',
						'conver'=>true,
						'resize'=>false,
						'drag'=>false,
						'btns'=>true,
						'btnText'=>'返回',
						'rightClose'=>false
				));
				$this->load->view("admin/$this->adminCurrentTemplate/error",$data);
				return false;
			}
			if(strlen($data['adminEditCon'])==0){
				unset($data['errorScript']['lhgdialog_alert_mess']);
				$data['errorScript'] = array('lhgdialog_alert_mess'=>array(
						'width'=>430,
						'height'=>108,
						'title'=>'后台添加/修改地址错误',
						'content'=>'<span class="red">对不起，后台添加/修改地址不能为空，请输入后台添加/修改地址！</span>',
						'url'=>'history.back();',
						'target'=>'_self',
						'conver'=>true,
						'resize'=>false,
						'drag'=>false,
						'btns'=>true,
						'btnText'=>'返回',
						'rightClose'=>false
				));
				$this->load->view("admin/$this->adminCurrentTemplate/error",$data);
				return false;
			}
			/*if(strlen($data['adminMainCon'])==0){
				unset($data['errorScript']['lhgdialog_alert_mess']);
				$data['errorScript'] = array('lhgdialog_alert_mess'=>array(
						'width'=>440,
						'height'=>108,
						'title'=>'后台档案管理地址错误',
						'content'=>'<span class="red">对不起，后台档案管理地址不能为空，请输入后台档案管理地址！</span>',
						'url'=>'history.back();',
						'target'=>'_self',
						'conver'=>true,
						'resize'=>false,
						'drag'=>false,
						'btns'=>true,
						'btnText'=>'返回',
						'rightClose'=>false
				));
				$this->load->view("admin/$this->adminCurrentTemplate/error",$data);
				return false;
			}*/
			if(strlen($data['adminEditCon'])==0){
				unset($data['errorScript']['lhgdialog_alert_mess']);
				$data['errorScript'] = array('lhgdialog_alert_mess'=>array(
						'width'=>440,
						'height'=>108,
						'title'=>'后台添加/修改地址错误',
						'content'=>'<span class="red">对不起，后台添加/修改地址不能为空，请输入后台添加/修改地址地址！</span>',
						'url'=>'history.back();',
						'target'=>'_self',
						'conver'=>true,
						'resize'=>false,
						'drag'=>false,
						'btns'=>true,
						'btnText'=>'返回',
						'rightClose'=>false
				));
				$this->load->view("admin/$this->adminCurrentTemplate/error",$data);
				return false;
			}
			
			if(strlen($data['id'])!=0){
				$this->db->trans_start();
				$oldSubjoinTable = $this->JayphaDB->getDBValue('subjoinTable',$this->dbTablePrefix.$tvName,array('id'=>$data['id']),'');
				if(strtolower($oldSubjoinTable)==strtolower($data['subjoinTable'])){
					$table_fields = array(
						'aid'=>array('type'=>'int','constraint'=>9,'unsigned'=>true,'comment'=>'主表ID')
						,'channelid'=>array('type'=>'int','constraint'=>9,'unsigned'=>true,'default'=>0,'comment'=>'频道ID')
						,'categoryid'=>array('type'=>'int','constraint'=>9,'unsigned'=>true,'default'=>0,'comment'=>'栏目ID')
					);
					$this->dbforge->add_key('aid',true);
					$this->dbforge->add_field($table_fields);
					$this->dbforge->create_table($this->dbTablePrefix.$data['subjoinTable'],true);
				}else{
					$this->dbforge->rename_table($this->dbTablePrefix.$oldSubjoinTable,$this->dbTablePrefix.$data['subjoinTable']);
				}
				
				if(strlen($data['adminMainCon'])==0){
					$data['adminMainCon'] = 'admin/archivescon/index/'.$data['id'];
				}
				
				$upData = array(
					'disabled'=>$data['disabled']
					,'title'=>$data['title']
					,'identifier'=>$data['identifier']
					,'mainTable'=>'archives'
					,'subjoinTable'=>$data['subjoinTable']
					,'useTitle'=>$data['useTitle']
					,'listfields'=>$data['listfields']
					,'adminEditCon'=>$data['adminEditCon']
					,'adminMainCon'=>$data['adminMainCon']
					,'userMainCon'=>$data['userMainCon']
					,'userEditCon'=>$data['userEditCon']
					,'fieldset'=>$data['fieldset']
				);
				$this->JayphaDB->updateDB($upData,$this->dbTablePrefix.$tvName,array('id'=>$data['id']));
				$this->db->trans_complete();
				unset($data['errorScript']['lhgdialog_alert_mess']);
				if($this->db->trans_status()===TRUE){
					$data['errorScript'] = array('lhgdialog_alert_mess'=>array(
							'width'=>240,
							'height'=>108,
							'title'=>'修改成功 ',
							'content'=>'恭喜您，内容模型修改成功！',
							'url'=>'location.reload',
							'target'=>'_self',
							'conver'=>true,
							'resize'=>false,
							'drag'=>false,
							'btns'=>true,
							'btnText'=>'关闭',
							'rightClose'=>false
					));
					$this->load->view("admin/$this->adminCurrentTemplate/error",$data);
					return false;
				}else{
					$this->db->trans_rollback();
					$data['errorScript'] = array('lhgdialog_alert_mess'=>array(
							'width'=>240,
							'height'=>108,
							'title'=>'修改失败 ',
							'content'=>'对不起，内容模型修改失败！',
							'url'=>'history.back()',
							'target'=>'_self',
							'conver'=>true,
							'resize'=>false,
							'drag'=>false,
							'btns'=>true,
							'btnText'=>'关闭',
							'rightClose'=>false
					));
					$this->load->view("admin/$this->adminCurrentTemplate/error",$data);
					return false;
				}
			}else{
				$addData = array(
					'disabled'=>$data['disabled']
					,'title'=>$data['title']
					,'identifier'=>$data['identifier']
					,'mainTable'=>'archives'
					,'subjoinTable'=>$data['subjoinTable']
					,'useTitle'=>$data['useTitle']
					,'listfields'=>$data['listfields']
					,'adminEditCon'=>$data['adminEditCon']
					,'adminMainCon'=>$data['adminMainCon']
					,'userMainCon'=>$data['userMainCon']
					,'userEditCon'=>$data['userEditCon']
					,'fieldset'=>$data['fieldset']
				);
				$this->db->trans_start();
				$this->JayphaDB->insertDB($this->dbTablePrefix.$tvName,$addData,true);
				if(strlen($data['adminMainCon'])==0){
					$channel_id = $this->db->insert_id();
					$updata = array('adminMainCon'=>'admin/archivescon/index/'.$channel_id);
					$this->JayphaDB->updateDB($updata,$this->dbTablePrefix.$tvName,array('id'=>$channel_id));
				}
				
				$table_fields = array(
					'aid'=>array('type'=>'int','constraint'=>9,'unsigned'=>true,'comment'=>'主表ID')
					,'channelid'=>array('type'=>'int','constraint'=>9,'unsigned'=>true,'default'=>0,'comment'=>'频道ID')
					,'categoryid'=>array('type'=>'int','constraint'=>9,'unsigned'=>true,'default'=>0,'comment'=>'栏目ID')
				);
				$this->dbforge->add_key('aid',true);
				$this->dbforge->add_field($table_fields);
				$this->dbforge->create_table($this->dbTablePrefix.$data['subjoinTable'],true);
				$this->db->trans_complete(); 				
				if($this->db->trans_status()===TRUE){
					unset($data['errorScript']['lhgdialog_alert_mess']);
					$data['errorScript'] = array('lhgdialog_alert_mess'=>array(
							'width'=>240,
							'height'=>108,
							'title'=>'添加成功 ',
							'content'=>'恭喜您，内容模型添加成功！',
							'url'=>'location.reload',
							'target'=>'_self',
							'conver'=>true,
							'resize'=>false,
							'drag'=>false,
							'btns'=>true,
							'btnText'=>'关闭',
							'rightClose'=>false
					));
					$this->load->view("admin/$this->adminCurrentTemplate/error",$data);
					return false;
				}else{
					$this->db->trans_rollback();
					unset($data['errorScript']['lhgdialog_alert_mess']);
					$data['errorScript'] = array('lhgdialog_alert_mess'=>array(
							'width'=>240,
							'height'=>108,
							'title'=>'添加失败 ',
							'content'=>'对不起，内容模型添加失败！',
							'url'=>'history.back()',
							'target'=>'_self',
							'conver'=>true,
							'resize'=>false,
							'drag'=>false,
							'btns'=>true,
							'btnText'=>'关闭',
							'rightClose'=>false
					));
					$this->load->view("admin/$this->adminCurrentTemplate/error",$data);
					return false;
				}
			}
		}else{
			$data['id'] = $this->uri->segment(4);
			if(strlen($data['id'])>0){
				$channelList = $this->JayphaDB->getDB(0,1,'',$this->dbTablePrefix.$tvName,array('id'=>$data['id']),'',false);
				if(count($channelList)!=0){
					foreach($channelList as $row){
						$data['disabled'] = $row->disabled;
						$data['title'] = $row->title;
						$data['identifier'] = $row->identifier;
						$data['subjoinTable'] = $row->subjoinTable;
						$data['useTitle'] = $row->useTitle;
						$data['listfields'] = $row->listfields;
						$data['adminEditCon'] = $row->adminEditCon;
						$data['adminMainCon'] = $row->adminMainCon;
						$data['userMainCon'] = $row->userMainCon;
						$data['userEditCon'] = $row->userEditCon;
						$data['fieldset'] = $row->fieldset;											
						if(strlen($data['fieldset'])!=0){
							$data['fieldsetList'] = $this->JayphaTemplate->getSubjoinFields($data['fieldset'],3);
						}
					}
				}
			}
		}
		$this->load->view("admin/$this->adminCurrentTemplate/channelEdit",$data);
	}
	
	function field(){
		$tvName = 'channel';
		$data['adminCurrentTemplate'] = $this->adminCurrentTemplate;
		$tvName = 'channel';
		$data['tvName'] = $tvName;
		$data['perm'] = 'channel';
		$data['db_table_prefix'] = $this->dbTablePrefix;
		$data['datatype'] = $this->datatype;
		$data['id'] = $this->uri->segment(4,0);
		$oldFieldname = $this->uri->segment(5);
		$subjoinTable = $this->JayphaDB->getDBValue('subjoinTable',$this->dbTablePrefix.$tvName,array('id'=>$data['id']),'');
		
		if($this->input->server('REQUEST_METHOD')=='POST'){
			$oldFieldset = $this->JayphaDB->getDBValue('fieldset',$this->dbTablePrefix.$tvName,array('id'=>$data['id']),'');		
			$data['autofield'] = $this->input->post('autofield');
			$data['isnull'] = $this->input->post('isnull');
			$data['type'] = $this->input->post('type');
			$data['itemname'] = $this->input->post('itemname');
			$data['fieldname'] = $this->input->post('fieldname');
			$data['maxlength'] = $this->input->post('maxlength');
			$data['default'] = $this->input->post('default');
			if($data['isnull']==1)
				$notnull = false;
			else
				$notnull = true;
			
			switch(strtolower($this->datacate[$data['type']])){
				case 'mediumtext':
					$field_arr[$data['fieldname']] = array('type'=>'mediumtext');
					if($notnull)
						$field_arr[$data['fieldname']]['not null'] = $notnull;
					break;
				case 'varchar':
				case 'char':
					$field_arr[$data['fieldname']] = array('type'=>'varchar','constraint'=>$data['maxlength']);
					if(!$notnull)
						$field_arr[$data['fieldname']]['default'] = $data['default'];
					else
						$field_arr[$data['fieldname']]['not null']= $notnull;
					break;
				case 'int':
				case 'float':
					$field_arr[$data['fieldname']] = array('type'=>$this->datacate[$data['type']]);
					if(!$notnull){
						if(strlen($data['default'])!=0){
							if(strtolower($this->datacate[$data['type']])=='int')
								$field_arr[$data['fieldname']]['constraint'] =$data['maxlength'];
							$field_arr[$data['fieldname']]['default'] = $data['default'];
						}else{
							if(strtolower($this->datacate[$data['type']])=='int')
								$field_arr[$data['fieldname']]['constraint'] = 6;
							$field_arr[$data['fieldname']]['default'] = 0;
						}
					}else{
						$field_arr[$data['fieldname']]['not null'] = $notnull;
					}
					break;
				case 'timestamp':
					$field_arr[$data['fieldname']] = array('type'=>'int','unsigned'=>true,'default'=>0);
					break;
				case 'set':
					$setlist = '';
					if(strstr($data['default'],'=')){
						$pattern = '/[\s\S].*=([\s\S].*)/i';
						$field__arr = preg_replace($pattern,'\1',explode(',',$data['default']));
						foreach($field__arr as $field__item){
							if(strlen($setlist)==0){
								$setlist .= "'$field__item'";
							}else{
								$setlist .= ",'$field__item'";
							}
						}
					}else{
						$setlist = "'".str_replace(',',"','",$data['default'])."'";
					}
					$field_arr[$data['fieldname']] = array('type'=>"set(".$setlist.")");
					break;
			}
			
			$fieldset = "<field:".$data['fieldname'];
			$fieldset .= " itemname='".$data['itemname']."'";
			$fieldset .= " autofield='".$data['autofield']."'";
			$fieldset .= " type='".$data['type']."'";
			$fieldset .= " maxlength='".$data['maxlength']."'";
			$fieldset .= " isnull='".$data['isnull']."'";
			$fieldset .= " default='".$data['default']."'";
			$fieldset .= "></field:".$data['fieldname'].">";
			$sub_mess_title = '修改字段结果';
			$sub_mess_content = '恭喜您，已经成功修改一个字段！';
			if(strlen($oldFieldname)==0){
				if($this->db->field_exists($data['fieldname'],$this->dbTablePrefix.$subjoinTable)){
					$this->dbforge->modify_column($this->dbTablePrefix.$subjoinTable,$field_arr);
				}else{
					$this->dbforge->add_column($this->dbTablePrefix.$subjoinTable,$field_arr);
				}
				$sub_mess_title = '添加字段结果';
				$sub_mess_content = '恭喜您，已经成功添加一个字段！';
				
				if(strlen($oldFieldset)!=0){
					$fieldset = $oldFieldset.$fieldset;
				}				
			}else{
				if(strlen($oldFieldset)!=0){
					$oldFieldset2 = $this->JayphaTemplate->getSubjoinFieldsByName($oldFieldname,$oldFieldset,0);
					$fieldset = str_ireplace($oldFieldset2,$fieldset,$oldFieldset);
				}
				if($this->db->field_exists($oldFieldname,$this->dbTablePrefix.$subjoinTable)){
					$field_arr[$oldFieldname] = $field_arr[$data['fieldname']];
					if(strtolower($oldFieldname)!=strtolower($data['fieldname']))
						unset($field_arr[$data['fieldname']]);								
					if(strtolower($oldFieldname)!=strtolower($data['fieldname']))
						$field_arr[$oldFieldname]['name'] = $data['fieldname'];										
					$this->dbforge->modify_column($this->dbTablePrefix.$subjoinTable,$field_arr);					
				}else{
					$this->dbforge->add_column($this->dbTablePrefix.$subjoinTable,$field_arr);					
				}
			}
			$upData = array('fieldset'=>$fieldset);
			$this->JayphaDB->updateDB($upData,$this->dbTablePrefix.$tvName,array('id'=>$data['id']));
			$errormsg = "<script type='text/javascript'>
			$(function(){
				subMess(258,108,'$sub_mess_title','$sub_mess_content');
			})
			var subMess = function(dialogWidth,dialogHeight,dialogTitle,dialogContent){
				var subMessDialog = new dg.curWin.$.dialog({		
						id:'subMess',
						width:dialogWidth,
						height:dialogHeight,
						cover: true,
						iconTitle:false,
						xButton:false,
						resize:false,
						rang:true,
						maxBtn:false,
						autoPos:{left:'center',top:'center'},
						cancelBtnTxt:'返回',
						onCancel:function(){
							dg.iWin('editMess').location.reload();
							dg.cancel();
						},
						title:dialogTitle,
						html:'<p class=\"mess-detail\">' + dialogContent + '</p>'
				});
				subMessDialog.ShowDialog();
			}
			</script>";
			$data['errorMsg'] = $errormsg;
			$this->load->view("admin/$this->adminCurrentTemplate/error",$data);
			return false;
		}else{
			$data['fieldname'] = $this->uri->segment(5);			
			if(strlen($data['fieldname'])>0){
				$subjoinTableAttr = array();
				if($data['id']!=0){
					$fieldset = $this->JayphaDB->getDB(0,1,'fieldset',$this->dbTablePrefix.$tvName,array('id'=>$data['id']),'',false);
					foreach($fieldset as $item){
						$subjoinTableAttr = $this->JayphaTemplate->getSubjoinFieldsByName($data['fieldname'],$item->fieldset,2);
					}
				}
				if(is_array($subjoinTableAttr)){
					$data = array_merge($data,$subjoinTableAttr);
				}
			}
		}
		$this->load->view("admin/$this->adminCurrentTemplate/channelField",$data);
	}
	
	function delField(){
		$data['adminCurrentTemplate'] = $this->adminCurrentTemplate;
		$tvName = 'channel';
		$data['tvName'] = $tvName;
		$data['perm'] = 'channel';
		$data['db_table_prefix'] = $this->dbTablePrefix;
		$data['datatype'] = $this->datatype;
		$data['id'] = $this->uri->segment(4,0);
		$data['fieldname'] = $this->uri->segment(5);
		if(strlen($data['fieldname'])>0 && $data['id']!=0){
			$subjoinTable = $this->JayphaDB->getDBValue('subjoinTable',$this->dbTablePrefix.$tvName,array('id'=>$data['id']),'');
			$fieldsets = $this->JayphaDB->getDBValue('fieldset',$this->dbTablePrefix.$tvName,array('id'=>$data['id']),'');
			$fieldset = $this->JayphaTemplate->getSubjoinFieldsByName($data['fieldname'],$fieldsets,0);
			$newFieldsets = str_ireplace($fieldset,'',$fieldsets);
			$upData = array('fieldset'=>$newFieldsets);
			$this->JayphaDB->updateDB($upData,$this->dbTablePrefix.$tvName,array('id'=>$data['id']));
			if($this->db->field_exists($data['fieldname'],$this->dbTablePrefix.$subjoinTable)){
				$this->dbforge->drop_column($this->dbTablePrefix.$subjoinTable,$data['fieldname']);
			}
			$errormsg = "<script type='text/javascript'>
			$(function(){
				subMess(200,108,'字段删除结果','恭喜您，字段删除成功！');
			})
			var subMess = function(dialogWidth,dialogHeight,dialogTitle,dialogContent){
				var subMessDialog = new dg.curWin.$.dialog({		
						id:'subMess',
						width:dialogWidth,
						height:dialogHeight,
						cover: true,
						iconTitle:false,
						xButton:false,
						resize:false,
						rang:true,
						maxBtn:false,
						autoPos:{left:'center',top:'center'},
						cancelBtnTxt:'返回',
						onCancel:function(){
							dg.iWin('editMess').location.reload();
							dg.cancel();
						},
						title:dialogTitle,
						html:'<p class=\"mess-detail\">' + dialogContent + '</p>'
				});
				subMessDialog.ShowDialog();
			}
			</script>";
			$data['errorMsg'] = $errormsg;
			$this->load->view("admin/$this->adminCurrentTemplate/error",$data);
			return false;
		}else{
			$errormsg = "<script type='text/javascript'>
			$(function(){
				subMess(200,108,'字段删除结果','<span class=\"red\">对不起，字段删除失败！</span>');
			})
			var subMess = function(dialogWidth,dialogHeight,dialogTitle,dialogContent){
				var subMessDialog = new dg.curWin.$.dialog({		
						id:'subMess',
						width:dialogWidth,
						height:dialogHeight,
						cover: true,
						iconTitle:false,
						xButton:false,
						resize:false,
						rang:true,
						maxBtn:false,
						autoPos:{left:'center',top:'center'},
						cancelBtnTxt:'返回',
						onCancel:function(){
							//dg.iWin('editMess').location.reload();
							dg.cancel();
						},
						title:dialogTitle,
						html:'<p class=\"mess-detail\">' + dialogContent + '</p>'
				});
				subMessDialog.ShowDialog();
			}
			</script>";
			$data['errorMsg'] = $errormsg;
			$this->load->view("admin/$this->adminCurrentTemplate/error",$data);
			return false;
		}		
	}
}


/* End of file channel.php */
/* Location: ./application/controllers/admin/channel.php */